package com.erp.erp_ui.ProductFjInfo;

import com.erp.erp_entitys.ProducFjInfo.ProducFjInfoEntity;
import com.erp.erp_entitys.ProducFjInfo.ProducFjInfoExample;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.ProducFjInfo.IProductFjInfoService;
import com.erp.erp_servers.ProductFjGe.IProductFjGeService;
import com.erp.erp_servers.ProductLog.IProductLogService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName OperationLogControl
 * @Description 产品附件信息
 * @Author songjizhen
 * @Date 2020-06-30 21:41
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "productfjinfo")
public class ProductFjInfoControl extends Base {
   @Autowired
    IProductFjInfoService productFjInfoService;
    @Autowired
    IProductLogService productLogService;

    @Autowired
    IProductFjGeService productFjGeService;
    /**
     * 根据产中编码获取部件信息
     * @param code
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody
    public R<List<ProducFjInfoEntity>> selectByPage(String code){
        try{
            ProducFjInfoExample example=new ProducFjInfoExample();
            ProducFjInfoExample.Criteria criteria = example.createCriteria();
            criteria.andProductCodeEqualTo(code);
            List<ProducFjInfoEntity> producFjInfoEntities = productFjInfoService.selectByExample(example);
            return R.build(producFjInfoEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据id批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "delfjinfo",method = RequestMethod.POST)
    @ResponseBody
    public  R<Integer> delfjinfo(String[] ids){
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            List<Integer> list=new ArrayList<>();
            for(int i=0;i<ids.length;i++){
                list.add(Integer.parseInt(ids[i].replace("[","").replace("]","")));
            }
            ProducFjInfoExample example=new ProducFjInfoExample();
            ProducFjInfoExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(list);
            List<ProducFjInfoEntity> pfs = productFjInfoService.selectByExample(example);

            int num = productFjInfoService.deleteByExample(example);


                productLogService.savelog(pfs.get(0).getProductCode(),"删除","删除产品部件",user.getName(),user.getCode());

            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 保存
     * @param pfe
     * @return
     */
    @RequestMapping(value = "savefj",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> savefj(@RequestBody ProducFjInfoEntity pfe){
        try {
            //处理编码空格问题
            if(StringUtils.isNotEmpty(pfe.getCode())){
                pfe.setCode(pfe.getCode().trim());
            }
            if(StringUtils.isNotEmpty(pfe.getOldCode())){
                pfe.setOldCode(pfe.getOldCode().replaceAll("%20", " "));
            }
            if(pfe.getId() == null || !pfe.getCode().equals(pfe.getOldCode())){
                //新增或新编码和老编码不一致需要校验编码重复
                //校验规格编码是否已存在 true:已存在 false:不存在
                boolean flag = checkSpecCodeIsExist(pfe.getCode());
                if(flag){
                    //规格编码不能重复
                    return R.buildError(ResultStatusEnum.SPECIFICATION_CODE_ITERATE);
                }
            }
            UserEntity user = (UserEntity)this.getBean("user");
            int num=0;
            if(pfe.getId()==null){
                 num = productFjInfoService.insertSelective(pfe);
                productLogService.savelog(pfe.getProductCode(),"新增","新增产品部件",user.getName(),user.getCode());
            }else{
                num=productFjInfoService.updateByPrimaryKeySelective(pfe);
                productFjGeService.updateFjGeZhuGe(pfe.getOldCode(),pfe.getCode(),pfe.getProductCode());
                productLogService.savelog(pfe.getProductCode(),"更新","更新产品部件",user.getName(),user.getCode());
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 校验规格编码是否已存在 true:已存在 false:不存在
     * @param specCode 规格编码
     * @return 规格编码是否已存在 true:已存在 false:不存在
     */
    private boolean checkSpecCodeIsExist(String specCode) {
        ProducFjInfoEntity req = new ProducFjInfoEntity();
        req.setCode(specCode);
        List<ProducFjInfoEntity> productSpecList = productFjInfoService.getSpecInfo(req);
        if (CollectionUtils.isEmpty(productSpecList)) {
            return false;
        } else {
            return true;
        }
    }
}
